package com.yiyw.herostory.handler;

import com.yiyw.herostory.bo.UserBo;
import com.yiyw.herostory.manage.ChannelGroupManage;
import com.yiyw.herostory.manage.UserManage;
import com.yiyw.herostory.proto.GameMsgProtocol;
import io.netty.channel.ChannelHandlerContext;

/**
 * @Author yiyw
 * @Description 用户进入事件处理器
 * @Date 2022/8/14 14:28
 **/
public class UserEntryCmdHandler implements ICmdHandler<GameMsgProtocol.UserEntryCmd> {

    @Override
    public void handler(ChannelHandlerContext ctx, GameMsgProtocol.UserEntryCmd message) {
        int id = ChannelGroupManage.getUserIdByChannel(ctx.channel());
        UserBo user = UserManage.getUser(id);
        if (user == null) {
            return;
        }
//        // 保存当前用户信息
//        UserManage.addUser(new UserBo(message.getUserId(), message.getHeroAvatar()));
//        // 将 channel 绑定用户Id
//        ChannelGroupManage.bingUserIdToChannel(ctx.channel(), message.getUserId());
        // 返回客户端信息
        GameMsgProtocol.UserEntryResult.Builder result = GameMsgProtocol.UserEntryResult.newBuilder();
        result.setUserId(user.getUserId());
        result.setHeroAvatar(user.getHeroAvatar());
        // 广播
        ChannelGroupManage.writeAndFlush(result.build());
    }
}
